package com.muqi.app.user.widget;

import com.muqi.app.qlearn.student.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class WxPraiseWindow extends PopupWindow implements OnClickListener{
	private View conentView;
	private ClickWxListener listener;
	private int mShowMorePopupWindowWidth;
	private int mShowMorePopupWindowHeight;
	private ImageView praiseView;
	private TextView praiseTxt;
	
	@SuppressLint("InflateParams") 
	public WxPraiseWindow(final Activity context, ClickWxListener listener) {
		this.listener = listener;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.pop_wx_praise, null);
		this.setContentView(conentView);
		this.setWidth(LayoutParams.WRAP_CONTENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		//this.setBackgroundDrawable(new BitmapDrawable());
		conentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        mShowMorePopupWindowWidth = conentView.getMeasuredWidth();
        mShowMorePopupWindowHeight = conentView.getMeasuredHeight();
		//this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.AnimationPreview);
		LinearLayout button1 = (LinearLayout) conentView.findViewById(R.id.btn_dianzan);
		LinearLayout button2 = (LinearLayout) conentView.findViewById(R.id.btn_pinglun);
		praiseView = (ImageView) conentView.findViewById(R.id.praise_logo);
		praiseTxt = (TextView) conentView.findViewById(R.id.praise_txt);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
	}

	/**   
	 * showPopupWidow
	 * @param parent
	 */
	public void showPopupWindow(View parent, boolean hasPraised) {
		if (!this.isShowing()) {
			if(hasPraised){
				praiseView.setImageResource(R.drawable.icon_wx_yizan);
				praiseTxt.setText("取消");
			}else{
				praiseView.setImageResource(R.drawable.icon_wx_zan);
				praiseTxt.setText("点赞");
			}
			int heightMoreBtnView = parent.getHeight();
			this.showAsDropDown(parent, -(mShowMorePopupWindowWidth + 15),
                    -(mShowMorePopupWindowHeight + heightMoreBtnView) / 2);
		} else {
			this.dismiss();
		}
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.btn_dianzan:
			listener.onWxClick(1);
			break;
		case R.id.btn_pinglun:
			listener.onWxClick(2);
			break;
		default:
			break;
		}
		this.setFocusable(false);
		this.dismiss();
	}
	
	public interface ClickWxListener{
		void onWxClick(int wxtype);
	}
}
